﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="recInvoiceManualEditform.aspx.cs" Inherits="SIPO.Web.Forms.Transaction.recInvoiceManualform" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Ubah Tagihan</title>
   
    <script src="<%= ResolveUrl("~/scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.validate.wrapper.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.inputmask.bundle.min.js") %>" type="text/javascript"></script>
     <style type="text/css">
        .ui-autocomplete
        {
            max-height: 100px;
            overflow-y: auto; /* prevent horizontal scrollbar */
            overflow-x: hidden; /* add padding to account for vertical scrollbar */
            padding-right: 20px;
        }
        .custom-combobox
        {
            position: relative;
            display: inline-block;
        }
        .custom-combobox-toggle
        {
            position: absolute;
            top: 0;
            bottom: 0;
            margin-left: -1px;
            padding: 0; /* support: IE7 */
            height: 1.7em;
            top: 0.1em;
        }
        .custom-combobox-input
        {
            margin: 0;
            padding: 0.3em;
        }
         .style2
         {
             width: 244px;
         }
         .style3
         {
             width: 50;
             height: 36px;
         }
         .style4
         {
             height: 36px;
         }
         .style5
         {
             width: 244px;
             height: 36px;
         }
        </style>
      <script type="text/javascript">
          

          $(document).ready(function () {
              $("#btnSave1").button({ icons: { primary: "ui-icon-disk"} });
              $("#btnClose").button({ icons: { primary: "ui-icon-close"} });
         
          });

    </script>
     <script type="text/javascript" language="javascript">

         (function ($) {

             $.widget("custom.combobox", {
                 _create: function () {
                     this.wrapper = $("<span>")
          .addClass("custom-combobox")
          .insertAfter(this.element);

                     this.element.hide();
                     this._createAutocomplete();
                     this._createShowAllButton();
                 },

                 _createAutocomplete: function () {
                     var selected = this.element.children(":selected"),
          value = selected.val() ? selected.text() : "";

                     var width = this.element.width() > 100 ? "style='width:" + this.element.width() + "px';" : "";

                     this.input = $("<input " + width + ">")
          .appendTo(this.wrapper)
          .val(value)
          .attr("title", "")
          .addClass("custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left")
          .autocomplete({
              autoFocus: true,
              selectFirst: true,
              delay: 0,
              minLength: 0,
              source: $.proxy(this, "_source")
          })
          .tooltip({
              tooltipClass: "ui-state-highlight"
          });

                     this._on(this.input, {
                         autocompleteselect: function (event, ui) {
                             ui.item.option.selected = true;
                             this._trigger("select", event, {
                                 item: ui.item.option
                             });

                             var idOfCaller = this.element.attr('id');
                             switch (idOfCaller.toLowerCase()) {
                                 case ("ddldebtor"):
                                     $('#ddlDebtor').change();
                                     break;
                                 case ("ddlsubsector"):
                                     $('#ddlSubSector').change();
                                     break;
                                 case ("ddlsubsubsector"):
                                     $('#ddlSubSubSector').change();
                                     break;
                                 default:
                                     break;
                             }

                         },

                         autocompletechange: "_removeIfInvalid"
                     });
                 },

                 _createShowAllButton: function () {
                     var input = this.input,
          wasOpen = false;

                     $("<a>")
          .attr("tabIndex", -1)
          .attr("title", "Tampilkan Semua Item")
          .tooltip()
          .appendTo(this.wrapper)
          .button({
              icons: {
                  primary: "ui-icon-triangle-1-s"
              },
              text: false
          })
          .removeClass("ui-corner-all")
          .addClass("custom-combobox-toggle ui-corner-right")
          .mousedown(function () {
              wasOpen = input.autocomplete("widget").is(":visible");
          })
          .click(function () {
              input.focus();

              // Close if already visible
              if (wasOpen) {
                  return;
              }

              // Pass empty string as value to search for, displaying all results
              input.autocomplete("search", "");
          });
                 },

                 _source: function (request, response) {
                     var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
                     response(this.element.children("option").map(function () {
                         var text = $(this).text();
                         if (this.value && (!request.term || matcher.test(text)))
                             return {
                                 label: text,
                                 value: text,
                                 option: this
                             };
                     }));
                 },

                 _removeIfInvalid: function (event, ui) {

                     // Selected an item, nothing to do
                     if (ui.item) {
                         return;
                     }

                     // Search for a match (case-insensitive)
                     var value = this.input.val(),
          valueLowerCase = value.toLowerCase(),
          valid = false;
                     this.element.children("option").each(function () {
                         if ($(this).text().toLowerCase() === valueLowerCase) {
                             this.selected = valid = true;
                             return false;
                         }
                     });

                     // Found a match, nothing to do
                     if (valid) {
                         return;
                     }

                     // Remove invalid value
                     this.input
          .val("")
          .attr("title", value + " tidak ditemukan")
          .tooltip("open");
                     this.element.val("");
                     this._delay(function () {
                         this.input.tooltip("close").attr("title", "");
                     }, 2500);
                     this.input.data("ui-autocomplete").term = "";
                 },

                 _destroy: function () {
                     this.wrapper.remove();
                     this.element.show();
                 },
                 autocomplete: function (value, text) {
                     this.element.val(value);
                     this.input.val(text);
                 }
             });
         })(jQuery);

         $(function () {

             $('#ddlDebtor').combobox();
             $('#ddlSubSector').combobox();
             $('#ddlSubSubSector').combobox();
         });

    </script>
    <script type="text/javascript">
        $('#btnClose').click(function () {
            parent.$('#dialog').dialog('close');
        });

    </script>
    <script type="text/javascript">

        $(document).ready(function () {

            //                $.validator.addMethod("dropDownValidator", function (value, element, parameters) {
            //                         return (value != '');
            var rules = {
                //                ddlDebtor: { dropDownValidator: true },
                //                ddlSubSubSector: { dropDownValidator: true },
                //                ddlJenisTransaksi: { dropDownValidator: true },
                //                ddlSubSector: { dropDownValidator: true },
                txtValue: "required",
                txtKeterangan: "required"
            };

            var messages = {
                //                ddlDebtor: "Debitur harus dipilih",
                //                ddlJenisTransaksi: "Jenis Transaksi harus dipilih",
                //                ddlSubSubSector: "Sub sub sektor harus dipilih",
                //                ddlSubSector: "Sub sektor harus dipilih",
                txtValue: "Nilai tagihan harus di isi",
                txtKeterangan: "Keterangan harus di isi"
            };
            // 2. Initiate the validator
            var validator = new jQueryValidatorWrapper("form1", rules, messages);
            //  GetDebtor();



            function Clear() {

                $('#ddlSubSubSector').combobox('autocomplete', "", "-- pilih sub sub sektor --");
                $('#ddlSubSector').combobox('autocomplete', "", "-- pilih sub sektor --");
            }

            $('#ddlSubSector').change(function () {
                GetSubSubSector();
            });

            $('#ddlDebtor').change(function () {
                //$('#<%= ddlSubSubSector.ClientID %>').combobox('autocomplete', "", "-- pilih sub sub sektor --");
                Clear();
                GetSubSector();
                //                $('#<%= ddlSubSubSector.ClientID %>').find('option:not(:first)').remove();
                //                var debtorId = $('#<%= ddlDebtor.ClientID%> option:selected').val();
                //                $subSubSectors = $('#<%= ddlSubSubSector.ClientID %>')
                //                $subSectors = $('#<%= ddlSubSector.ClientID%>');
                //                if (debtorId > 0) {
                //                    var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getsubsubsector.ashx?method=getbyDebtorId&debtorid=") %>';
                //                    $.ajax({
                //                        type: 'POST',
                //                        url: handlerUrl + debtorId,
                //                        contentType: 'application/json',
                //                        dataType: 'json',
                //                        success: function (datasubsubs) {
                //                            if (datasubsubs.length > 1) {
                //                                $subSubSectors.get(0).options.length = 0;
                //                                $subSubSectors.get(0).options[0] = new Option("-- pilih sub sub sektor --", "");
                //                                $.each(datasubsubs, function (i, datasubsub) {
                //                                    $subSubSectors.append('<option value="' + datasubsub.SubSubSectorID + '">' + datasubsub.SubSubSectorCode + ' - ' + datasubsub.SubSubSectorName + '</option>');
                //                                });
                //                                $subSubSectors.combobox('autocomplete', "", "-- pilih sub sub sektor --");
                //                            }
                //                            else {
                //                                $subSubSectors.append('<option value="' + datasubsubs[0].SubSubSectorID + '">' + datasubsubs[0].SubSubSectorCode + ' - ' + datasubsubs[0].SubSubSectorName + '</option>');
                //                                $subSubSectors.combobox('autocomplete', datasubsubs[0].SubSubSectorID, datasubsubs[0].SubSubSectorCode + ' - ' + datasubsubs[0].SubSubSectorName);
                //                                //$subSubSectors.trigger("change");
                //                            }
                //                            GetSubSector();
                //                        },
                //                        error: function () {
                //                            alert('Error!');
                //                        }
                //                    });
                //                }
                //                else {
                //                    $subSubSectors.get(0).options.length = 0;
                //                    $subSubSectors.get(0).options[0] = new Option("-- pilih sub sub sektor --", "");
                //                }
            });

            $('#txtValue').inputmask("decimal", { radixPoint: ",", autoGroup: true, groupSeparator: ".", groupSize: 3, digits: 2, repeat: 18 });



            //            function GetDebtor() {
            //                var debtorId = "<%= base.DebtorID %>";
            //                var userTypeId = "<%= base.UserTypeID %>";
            //                var subsubsectors = "<%= base.SubSubSectorIDs %>";
            //                var handlerUrl = "";
            //                if (userTypeId == 2) {
            //                    handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getdebtor.ashx?method=getbyid&debtorID=' + debtorId + '") %>';
            //                }
            //                else if (userTypeId == 3) {
            //                    handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getdebtor.ashx?method=getbysubsubsector&subsubsectors=' + subsubsectors + '") %>';
            //                }
            //                $debtor = $('#<%= ddlDebtor.ClientID %>');
            //                //$('#ddlDebtor').find('option:not(:first)').remove();
            //                $.ajax({
            //                    type: 'POST',
            //                    url: handlerUrl,
            //                    contentType: 'application/json',
            //                    dataType: 'json',
            //                    success: function (datas) {
            //                        if (datas.length > 1) {
            //                            $debtor.get(0).options[0] = new Option("-- pilih debitur --", "");
            //                            $.each(datas, function (i, data) {
            //                                $debtor.append('<option value="' + data.DebtorID + '">' + data.DebtorCode + ' - ' + data.DebtorName + '</option>');
            //                            });
            //                            $debtor.combobox('autocomplete', "", "-- pilih debitur --");
            //                            //$debtor.parent().find("a.ui-button").button("disable");
            //                        }
            //                        else {
            //                            $debtor.append('<option value="' + datas[0].DebtorID + '">' + datas[0].DebtorCode + ' - ' + datas[0].DebtorName + '</option>');
            //                            $debtor.combobox('autocomplete', datas[0].DebtorID, datas[0].DebtorCode + ' - ' + datas[0].DebtorName);

            //                            //GetSubSubSector();

            //                        }
            //                        GetSubSector();
            //                    },
            //                    error: function () {
            //                        alert('Error load debitur!');
            //                    }
            //                });
            //            }

            function GetSubSubSector() {
                //  $('#<%= ddlSubSubSector.ClientID %>').find('option:not(:first)').remove();
                var debtorId = $('#ddlDebtor option:selected').val();
                var subsectorid = $('#ddlSubSector option:selected').val();
                $subSubSectors = $('#ddlSubSubSector')
                if (subsectorid > 0) {
                    var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getsubsubsector.ashx?method=getbydebtorid_subsectorid_AND_subsubsectorids&subsector_id=' + subsectorid + '&debtorid=' + debtorId + ' ") %>';
                    $.ajax({
                        type: 'POST',
                        //url: handlerUrl + debtorId,
                        url: handlerUrl,
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (datasubsubs) {
                            if (datasubsubs.length > 1) {
                                $subSubSectors.get(0).options.length = 0;
                                $subSubSectors.get(0).options[0] = new Option("-- pilih sub sub sektor --", "");
                                $.each(datasubsubs, function (i, datasubsub) {
                                    $subSubSectors.append('<option value="' + datasubsub.SubSubSectorID + '">' + datasubsub.SubSubSectorCode + ' - ' + datasubsub.SubSubSectorName + '</option>');
                                });
                                $subSubSectors.combobox('autocomplete', "", "-- pilih sub sub sektor --");
                            }
                            else {
                                $subSubSectors.append('<option value="' + datasubsubs[0].SubSubSectorID + '">' + datasubsubs[0].SubSubSectorCode + ' - ' + datasubsubs[0].SubSubSectorName + '</option>');
                                $subSubSectors.combobox('autocomplete', datasubsubs[0].SubSubSectorID, datasubsubs[0].SubSubSectorCode + ' - ' + datasubsubs[0].SubSubSectorName);
                                $subSubSectors.trigger("change");
                            }
                        },
                        error: function () {
                            alert('Error!');
                        }
                    });
                }
                else {
                    $subSubSectors.get(0).options.length = 0;
                    $subSubSectors.get(0).options[0] = new Option("-- pilih sub sub sektor --", "");
                }
            }

            function GetSubSector() {
                var debtorid = $("#ddlDebtor option:selected").val();
                $subSectors = $('#ddlSubSector');
                $subSectors.get(0).options.length = 0;
                $subsubsector = $('#ddlSubSubSector');
                // $('#ddlDebtorSector').find("option").remove();
                // $('#ddlSubSubSector').find('option:not(:first)').remove();
                // $('#ddlSubSector').find('option:not(:first)').remove();
                if (debtorid > 0) {
                    // ambil data sub sector dari tabel debtor sector where debtor id
                    var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getsubsector.ashx?method=getbydebtorid") %>';
                    $.ajax({
                        type: 'POST',
                        url: handlerUrl + "&debtor_id=" + debtorid,
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (datas) {
                            if (datas.length > 1) {
                                //refresh combo sub sub sector
                                $subsubsector.get(0).options.length = 0;
                                $subsubsector.get(0).options[0] = new Option("-- pilih sub sub sektor --", "");
                                $subsubsector.combobox('autocomplete', "", "-- pilih sub sub sektor --");
                                //
                                $subSectors.get(0).options[0] = new Option("-- pilih sub sektor --", "");
                                $.each(datas, function (i, data) {
                                    $subSectors.append('<option value="' + data.SubSectorID + '">' + data.SubSectorCode + ' - ' + data.SubSectorName + '</option>');
                                });
                                $subSectors.combobox('autocomplete', "", "-- pilih sub sektor --");
                            }
                            else {
                                $subSectors.append('<option value="' + datas[0].SubSectorID + '">' + datas[0].SubSectorCode + ' - ' + datas[0].SubSectorName + '</option>');
                                $subSectors.combobox('autocomplete', datas[0].SubSectorID, datas[0].SubSectorCode + ' - ' + datas[0].SubSectorName);
                                //$subSubSectors.trigger("change");
                            }
                            GetSubSubSector();
                        },
                        error: function () {
                            alert('Error load sub sector from debtor sector!');
                        }
                    });
                }
            }


            $("#btnSave1").click(function () {

                if (!validator.validate())
                    return;

                var SubSubSector = $('#ddlSubSubSector').val();
                var SubSector = $('#ddlSubSector').val();
                var Debtor = $('#ddlDebtor').val();
                var JenTrans = $('#ddlJenisTransaksi').val();
                var SValue = $('#txtValue').val();
                var Keterangan = $('#txtKeterangan').val();
                var ID = $('#hfID').val();
                var DueDate = $('#hfDueDate').val();
                var BillDate = $('#hfBillDate').val();
                if (Debtor != "" && JenTrans != "" && SubSector != "" && SubSubSector != "") {
                    var objrecBilling = new Object();
                    objrecBilling.SubSubSectorID = encodeURIComponent(SubSubSector);
                    objrecBilling.DebtorID = encodeURIComponent(Debtor);
                    objrecBilling.TransTypeID = encodeURIComponent(JenTrans);
                    objrecBilling.Value = encodeURIComponent(SValue);
                    objrecBilling.Keterangan = encodeURIComponent(Keterangan);
                    objrecBilling.BillingNo = ID;
                    objrecBilling.DueDate = DueDate;
                    objrecBilling.BillDate = BillDate;

                    jsonData = JSON.stringify(objrecBilling);

                    var handlerUrl = '<%= ResolveUrl("~/ajaxhandler/Transaction/recBillinghandler.ashx") %>';


                    CallAjax(handlerUrl, "?method=update", jsonData);
                    parent.$('#dialog').dialog('close');

                    setTimeout("parent.$('#table').trigger('reloadGrid');", 3000);


                    return false;
                }
                else {
                    alert('Lengkapi data yang belum terisi !');
                }
            });


        });

    </script>

    </head>
<body>
<form id="form1" runat="server">
    <table border="0">
        <tr>
            <td class="style3">
                Jenis Transaksi&nbsp;
            </td>
            <td class="style4">
                :
            </td>
            <td class="style5">
                &nbsp;<asp:DropDownList ID="ddlJenisTransaksi" runat="server" 
                    Height="22px" Width="216px">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td class="style4">
                Debitur</td>
            <td class="style4">
                :
            </td>
            <td class="style5">
                &nbsp;<asp:DropDownList ID="ddlDebtor" runat="server" 
                    Height="22px" Width="216px">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td class="style4">
                Sub Sektor
            </td>
            <td class="style4">
                :
            </td>
            <td class="style5">
               &nbsp;<asp:DropDownList ID="ddlSubSector" runat="server" 
                    Height="22px" Width="216px">
                </asp:DropDownList>
            </td>
        </tr>
         <tr>
            <td class="style4">
                Sub Sub Sektor
            </td>
            <td class="style4">
                :
            </td>
            <td class="style5">
               &nbsp;<asp:DropDownList ID="ddlSubSubSector" runat="server" 
                    Height="22px" Width="216px">
                </asp:DropDownList>
             </td>
        </tr>
         <tr>
            <td class="style4">
                Jumlah</td>
            <td class="style4">
                :
            </td>
            <td class="style5">
                &nbsp;<asp:TextBox ID="txtValue" runat="server" Height="22px" Width="173px"></asp:TextBox>
             </td>
        </tr>
         <tr>
            <td>
                Keterangan</td>
            <td>
                :
            </td>
            <td class="style2">
                &nbsp;<asp:TextBox ID="txtKeterangan" 
                    runat="server" Height="72px" TextMode="MultiLine" 
                    Width="244px"></asp:TextBox>
             </td>
        </tr>
        
        
        <tr>
            <td colspan="2">
                &nbsp;
            </td>
            <td style="width: 244px">
                       
                <div style="text-align: right; padding-top: 10px;">
                    <button id="btnSave1" type="button">
                        Simpan
                    </button>
                    <button id="btnClose" type="button">
                        Batal
                    </button>
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                &nbsp;
            </td>
            <td style="width: 244px">
                <asp:HiddenField ID="hfID" runat="server" />
                <asp:HiddenField ID="hfDueDate" runat="server" />
                <asp:HiddenField ID="hfBillDate" runat="server" />
            </td>
        </tr>
    </table>
      </form>
    </body>
</html>

